<?php
/**
	* wechat php test
*/



define("TOKEN", "zhangxingdong");//define your token
$wechatObj = new wechatCallback();
if(!empty($_GET['echostr'])&&!empty($_GET["signature"])&&!empty($_GET["nonce"])){
	$wechatObj->valid();
}else{
	$wechatObj->responseMsg();
}

class wechatCallback
{
	public function valid()
	{
		$echoStr = $_GET["echostr"];

		//valid signature , option
		if($this->checkSignature()){
			echo $echoStr;
			exit;
		}
	}

	public function responseMsg()
	{
		//include_once("get_and_post.php");
		$get_and_post = new get_and_post();
		include_once("tpl.php");

		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

		//extract post data
		if (!empty($postStr)){
				/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
					the best way is to check the validity of xml by yourself */
				libxml_disable_entity_loader(true);
				$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
				$fromUsername = $postObj->FromUserName;
				$toUsername = $postObj->ToUserName;
				$msgType = $postObj->MsgType;
				$keyword = trim($postObj->Content);
				$time = time();
				if($msgType=="event"){
					$event = $postObj->Event;
					$eventKey = $postObj->EventKey;
					if($event = "subscribe"){
						$contentStr = "欢迎关注星懂海！\n1、免费WiFi：直接发送位置信息\n2、聊天：您可以发送语音或普通文字消息与小机器人互动\n3、获取帮助：回复“帮助”或“help”";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						echo $resultStr;
						exit;
					}else{
						$contentStr = "...";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						echo $resultStr;
						exit;
					}
				}elseif($msgType=="image"){
					$contentStr = "图片消息";
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}elseif($msgType=="voice"){
					$recognition = $postObj->Recognition;

					header('Content-type:text/html;charset=utf-8');
					$url = "http://www.tuling123.com/openapi/api";
					$arr = array ('key'=>'b685c74beb9ca919fe027bf368f921a6','info'=>$recognition,'userid'=>$fromUsername);
					$user_post_data = json_encode($arr);
					$resultJson = $get_and_post->http_post_json($url,$user_post_data);
					$contentStr = json_decode($resultJson, true);
					$contentStr = $contentStr['text'];
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}elseif($msgType=="video"){
					$contentStr = "视频消息";
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}elseif($msgType=="shortvideo"){
					$contentStr = "小视频消息";
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}elseif($msgType=="location"){
					$lat = $postObj->Location_X;
					$lon = $postObj->Location_Y;

					/* $url = "http://apis.juhe.cn/wifi/local";
					$arr = array ('key'=>'3e779cd76d1fb42b17f768b731c09f93','lat'=>$lat,'lon'=>$lon,'gtype'=>2);
					$user_post_data = json_encode($arr);
					$resultJson = $get_and_post->http_post_json($url,$user_post_data); */
					$url = "http://apis.juhe.cn/wifi/local?key=3e779cd76d1fb42b17f768b731c09f93&lat=".$lat."&lon=".$lon."&gtype=2";
					$resultJson = $get_and_post->getCurl($url);
					$result = json_decode($resultJson, true);
					if($result){
						if($result['error_code']=='0'){
							switch(count($result['result']['data'])){
								case 0:
									$contentStr = "不好意思，您的附近3000米内没有免费wifi！";
									$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
									echo $resultStr;
									break;
								case 1:
									$resultStr = sprintf($news2Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 2:
									$resultStr = sprintf($news3Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 3:
									$resultStr = sprintf($news4Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 4:
									$resultStr = sprintf($news5Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 5:
									$resultStr = sprintf($news6Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 6:
									$resultStr = sprintf($news7Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "", "名称:".$result['result']['data'][5]['name']."\n介绍:".$result['result']['data'][5]['intro']."\n地址:".$result['result']['data'][5]['address']."\n距离:".$result['result']['data'][5]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 7:
									$resultStr = sprintf($news8Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "", "名称:".$result['result']['data'][5]['name']."\n介绍:".$result['result']['data'][5]['intro']."\n地址:".$result['result']['data'][5]['address']."\n距离:".$result['result']['data'][5]['distance']."米", "", "", "", "名称:".$result['result']['data'][6]['name']."\n介绍:".$result['result']['data'][6]['intro']."\n地址:".$result['result']['data'][6]['address']."\n距离:".$result['result']['data'][6]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 8:
									$resultStr = sprintf($news9Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "", "名称:".$result['result']['data'][5]['name']."\n介绍:".$result['result']['data'][5]['intro']."\n地址:".$result['result']['data'][5]['address']."\n距离:".$result['result']['data'][5]['distance']."米", "", "", "", "名称:".$result['result']['data'][6]['name']."\n介绍:".$result['result']['data'][6]['intro']."\n地址:".$result['result']['data'][6]['address']."\n距离:".$result['result']['data'][6]['distance']."米", "", "", "", "名称:".$result['result']['data'][7]['name']."\n介绍:".$result['result']['data'][7]['intro']."\n地址:".$result['result']['data'][7]['address']."\n距离:".$result['result']['data'][7]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								case 9:
									$resultStr = sprintf($news10Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "", "名称:".$result['result']['data'][5]['name']."\n介绍:".$result['result']['data'][5]['intro']."\n地址:".$result['result']['data'][5]['address']."\n距离:".$result['result']['data'][5]['distance']."米", "", "", "", "名称:".$result['result']['data'][6]['name']."\n介绍:".$result['result']['data'][6]['intro']."\n地址:".$result['result']['data'][6]['address']."\n距离:".$result['result']['data'][6]['distance']."米", "", "", "", "名称:".$result['result']['data'][7]['name']."\n介绍:".$result['result']['data'][7]['intro']."\n地址:".$result['result']['data'][7]['address']."\n距离:".$result['result']['data'][7]['distance']."米", "", "", "", "名称:".$result['result']['data'][8]['name']."\n介绍:".$result['result']['data'][8]['intro']."\n地址:".$result['result']['data'][8]['address']."\n距离:".$result['result']['data'][8]['distance']."米", "", "", "");
									echo $resultStr;
									break;
								default:
									$resultStr = sprintf($news10Tpl, $fromUsername, $toUsername, $time, "免费wifi《服务声明》", "", "http://weixin-img.zhangxingdong.cn/wifi.png", "http://weixin-img.zhangxingdong.cn/service.html", "名称:".$result['result']['data'][0]['name']."\n介绍:".$result['result']['data'][0]['intro']."\n地址:".$result['result']['data'][0]['address']."\n距离:".$result['result']['data'][0]['distance']."米", "", "", "", "名称:".$result['result']['data'][1]['name']."\n介绍:".$result['result']['data'][1]['intro']."\n地址:".$result['result']['data'][1]['address']."\n距离:".$result['result']['data'][1]['distance']."米", "", "", "", "名称:".$result['result']['data'][2]['name']."\n介绍:".$result['result']['data'][2]['intro']."\n地址:".$result['result']['data'][2]['address']."\n距离:".$result['result']['data'][2]['distance']."米", "", "", "", "名称:".$result['result']['data'][3]['name']."\n介绍:".$result['result']['data'][3]['intro']."\n地址:".$result['result']['data'][3]['address']."\n距离:".$result['result']['data'][3]['distance']."米", "", "", "", "名称:".$result['result']['data'][4]['name']."\n介绍:".$result['result']['data'][4]['intro']."\n地址:".$result['result']['data'][4]['address']."\n距离:".$result['result']['data'][4]['distance']."米", "", "", "", "名称:".$result['result']['data'][5]['name']."\n介绍:".$result['result']['data'][5]['intro']."\n地址:".$result['result']['data'][5]['address']."\n距离:".$result['result']['data'][5]['distance']."米", "", "", "", "名称:".$result['result']['data'][6]['name']."\n介绍:".$result['result']['data'][6]['intro']."\n地址:".$result['result']['data'][6]['address']."\n距离:".$result['result']['data'][6]['distance']."米", "", "", "", "名称:".$result['result']['data'][7]['name']."\n介绍:".$result['result']['data'][7]['intro']."\n地址:".$result['result']['data'][7]['address']."\n距离:".$result['result']['data'][7]['distance']."米", "", "", "", "点击进入查看更多>>>", "", "", "http://www.zhangxingdong.cn/weixin/wifi.php?lat=".$lat."&lon=".$lon);
									echo $resultStr;
									break;
							}
						}else{
							$contentStr = $result['error_code'].":".$result['reason'];
							$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
							echo $resultStr;
							exit;
						}
					}else{
						$contentStr = "请求失败";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						echo $resultStr;
						exit;
					}
				}elseif($msgType=="link"){
					$contentStr = "链接消息";
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}elseif($msgType=="text"){
					if(!empty( $keyword ))
					{
						if(strtolower($keyword)=="help"||$keyword=="帮助"){
							$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, "1、免费WiFi：直接发送位置信息\n2、聊天：您可以发送语音或普通文字消息与小机器人互动\n3、获取帮助：回复“帮助”或“help”");
							echo $resultStr;
							exit;
						}else{
							header('Content-type:text/html;charset=utf-8');
							$url = "http://www.tuling123.com/openapi/api";
							$arr = array ('key'=>'b685c74beb9ca919fe027bf368f921a6','info'=>$keyword,'userid'=>$fromUsername);
							$user_post_data = json_encode($arr);
							$resultJson = $get_and_post->http_post_json($url,$user_post_data);
							/* $url = "http://www.tuling123.com/openapi/api?key=b685c74beb9ca919fe027bf368f921a6&info=".$keyword."&userid=".$fromUsername;
							$resultJson = $get_and_post->getCurl($url); */
							$contentStr = json_decode($resultJson, true);
							$contentStr = $contentStr['text'];
							$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
							echo $resultStr;
							exit;
						}
					}else{
						$contentStr = "拿个“0”来忽悠我？";
						$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
						echo $resultStr;
						exit;
					}
				}else{
					$contentStr = "未知消息类型！\n你是外星来的吗？";
					$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
					echo $resultStr;
					exit;
				}
		}else {
			$time = time();
			$contentStr = "这是什么鬼？";
			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
			echo $resultStr;
			exit;
		}
	}

	private function checkSignature()
	{
		// you must define TOKEN by yourself
		if (!defined("TOKEN")) {
			throw new Exception('TOKEN is not defined!');
		}

		$signature = $_GET["signature"];
		$timestamp = $_GET["timestamp"];
		$nonce = $_GET["nonce"];

		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		// use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );

		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

class get_and_post
{
	public function getCurl($url){//get https的内容
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result =  curl_exec($ch);
		curl_close ($ch);
		return $result;
	}

	public function dataPost($url, $post_data = ''){//curl
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_POST, 1);
		if($post_data != ''){
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_HEADER, false);
		$file_contents = curl_exec($ch);
		curl_close($ch);
		return $file_contents;
	}

	public function http_post_json($url, $jsonStr){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: ' . strlen($jsonStr)
			)
		);
		$response = curl_exec($ch);
		//$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);

		//return array($httpCode, $response);
		return $response;
	}
}
?>